//
//  TicketList.m
//  pa
//
//  Created by HaiJiao Chen on 12-7-28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "TicketList.h"
#import "AppDelegate.h"
#import "ASIHTTPRequest.h"
#import "FilePack.h"
#import "CHJAlertView.h"
#import "XMLObject.h"
#import "LoginServer.h"
#import "ImageDownloader.h"
#import "GoogleMapDownloader.h"
#include "text_def.h"

NSString *const DRIVER_NOTE = @"DRIVER_NOTE";
//NSString *const ADD_WATER_BY = @"ADD_WATER_BY";
NSString *const ADD_WATER_GAL = @"ADD_WATER_GAL";
NSString *const STR_TIME_LEFT_PLANT = @"STR_TIME_LEFT_PLANT";
NSString *const STR_TIME_ARRIVED_JOB = @"STR_TIME_ARRIVED_JOB";
NSString *const STR_TIME_START_POUR = @"STR_TIME_START_POUR";
NSString *const STR_TIME_FINISHED_POUR = @"STR_TIME_FINISHED_POUR";
NSString *const STR_TIME_LEFT_JOB = @"STR_TIME_LEFT_JOB";
NSString *const STR_TIME_ARRIVED_PLANT = @"STR_TIME_ARRIVED_PLANT";
NSString *const STR_TIME_ELAPSED = @"STR_TIME_ELAPSED";
NSString *const C_TIME_LEFT_PLANT = @"C_TIME_LEFT_PLANT";
NSString *const C_TIME_ARRIVED_JOB = @"C_TIME_ARRIVED_JOB";
NSString *const C_TIME_START_POUR = @"C_TIME_START_POUR";
NSString *const C_TIME_FINISHED_POUR = @"C_TIME_FINISHED_POUR";
NSString *const C_TIME_LEFT_JOB = @"C_TIME_LEFT_JOB";
NSString *const C_TIME_ARRIVED_PLANT = @"C_TIME_ARRIVED_PLANT";
NSString *const C_TIME_ELAPSED = @"C_TIME_ELAPSED";
NSString *const ACCEPT_TERMS = @"ACCEPT_TERMS";
NSString *const SIGN_NAME = @"SIGN_NAME";
NSString *const SIGNATURE = @"SIGNATURE";
NSString *const LOGO_URL = @"LOGO_URL";
NSString *const PLANT_ADDR = @"PLANT_ADDR";
NSString *const EMAILS = @"EMAILS";
NSString *const CUSTOM_EMAILS = @"CUSTOM_EMAILS";
NSString *const IS_GONING = @"IS_GONING";
NSString *const CURRENT_TIME_INDEX = @"CURRENT_TIME_INDEX";
NSString *const BATCH_WEIGHT = @"BATCH_WEIGHT";
NSString *const HIDE_BATCH_WEIGHT = @"HIDE_BATCH_WEIGHT";
NSString *const REJECT_NAME = @"REJECT_NAME";
NSString *const REJECT_REASON = @"REJECT_REASON";


#warning MUST_HAVE_BW
NSString *const DONT_NEED_BW = @"DONT_NEED_BW";

@implementation TicketList
@synthesize data;
@synthesize memoryObject;

//- (NSString*)savePath
//{
//    return [[AppDelegate sharedInstance].cachePath stringByAppendingFormat:@"/ticket_%@.plist", [[AppDelegate sharedInstance] uid]];
//}
//
//- (NSString*)saveCustomPath
//{
//    return [[AppDelegate sharedInstance].cachePath stringByAppendingFormat:@"/custom_ticket_%@.plist", [[AppDelegate sharedInstance] uid]];
//}

- (id)init
{
    self = [super init];
    if (self)
    {
        get_logo_requests = [[NSMutableArray alloc] initWithCapacity:10];
        get_batchWeight_requests = [[NSMutableArray alloc] initWithCapacity:10];
        get_plat_addr_requests = [[NSMutableArray alloc] initWithCapacity:10];
        get_emails_requests = [[NSMutableArray alloc] initWithCapacity:10];
        
        normalQuene = [[ASINetworkQueue alloc] init];
        [normalQuene setDelegate:self];
        [normalQuene setRequestDidFinishSelector:@selector(requestFinished:)];
        [normalQuene setRequestDidFailSelector:@selector(requestFailed:)];
        normalQuene.shouldCancelAllRequestsOnFailure = NO;
        [normalQuene go];
        
        data = [[NSMutableArray alloc] initWithCapacity:10];
        //[data addObjectsFromArray:[NSArray arrayWithContentsOfFile:[self savePath]]];
        
        memoryObject = [[NSMutableDictionary alloc] initWithCapacity:10];
    }
    return self;
}

- (void)dealloc
{
    for (ASIHTTPRequest* request in get_logo_requests)
    {
        [[AppDelegate sharedInstance] cancelRequest:request];
    }
    for (ASIHTTPRequest* request in get_batchWeight_requests)
    {
        [[AppDelegate sharedInstance] cancelRequest:request];
    }
    for (ASIHTTPRequest* request in get_plat_addr_requests)
    {
        [[AppDelegate sharedInstance] cancelRequest:request];
    }
    for (ASIHTTPRequest* request in get_emails_requests)
    {
        [[AppDelegate sharedInstance] cancelRequest:request];
    }
    [get_logo_requests release];
    [get_batchWeight_requests release];
    [get_plat_addr_requests release];
    [get_emails_requests release];
    [data release];
    [normalQuene cancelAllOperations];
    [normalQuene setDelegate:nil];
    [normalQuene release];
    [[AppDelegate sharedInstance] cancelRequest:refresh_request];
    
    [bwc cancel]; [bwc release]; bwc = nil;
    [bwd release]; bwd = nil;
    bwid = 0;
    [super dealloc];
}

//- (void)requestFinished:(ASIHTTPRequest *)request
//{
//    if (request.userInfo == nil)
//    {
//        XMLObject* xml = [XMLObject parse:[request rawResponseData]];
//        XMLObject* tickets = [xml getXMLObjectForKey:@"Tickets"];
//        NSMutableArray* ser = [NSMutableArray array];
//        for (int i = 0; i < [tickets getCount]; i++)
//        {
//            XMLObject* obj = [tickets getXMLObjectForIndex:i];
//            NSMutableDictionary* sub = [obj convert2Dictionary];
//            [sub setValue:[[obj getXMLObjectForKey:@"items"] convert2Array] forKey:@"items"];
//            [ser addObject:sub];
//        }
//        
//        if ([xml getIntForKey:@"ret" defaultValue:0] == 1)
//        {
//            NSMutableArray* changed = [NSMutableArray array];
//            for (int i = 0; i < [ser count]; i++)
//            {
//                NSDictionary* dic = [ser objectAtIndex:i];
//                long long tickedIDi = [[dic valueForKey:@"TicketID"] longLongValue];
//                BOOL find = NO;
//                for (int j = 0; j < [data count]; j++)
//                {
//                    NSDictionary* jd = [data objectAtIndex:j];
//                    long long tickedIDj = [[jd valueForKey:@"TicketID"] longLongValue];
//                    
//                    if (tickedIDi == tickedIDj)
//                    {
//                        [data replaceObjectAtIndex:j withObject:dic];
//                        find = YES;
//                        break;
//                    }
//                }
//                if (find == NO)
//                {
//                    [data addObject:dic];
//                }
//                [changed addObject:[NSNumber numberWithLongLong:tickedIDi]];
//                
//                if ([memoryObject valueForKey:[NSString stringWithFormat:@"%lld", tickedIDi]] == nil)
//                {
//                    [memoryObject setValue:[NSMutableDictionary dictionary] forKey:[NSString stringWithFormat:@"%lld", tickedIDi]];
//                }
//            }
//            
//            [[NSNotificationCenter defaultCenter] postNotificationName:DataSourceChangeNotification object:self userInfo:[NSDictionary dictionaryWithObject:changed forKey:@"changed"]];
//            for (NSDictionary* dic in data)
//            {
//                long long ticketID = [[dic valueForKey:@"TicketID"] longLongValue];
//                NSMutableDictionary* c = [custom objectForKey:[NSString stringWithFormat:@"%lld", ticketID]];
//                NSString* logo = [c valueForKey:LOGO_URL];
//                if (logo == nil)
//                {
//                    [[AppDelegate sharedInstance] sendHttpRequest:[NSString stringWithFormat:@"getLogo?&ticketID=%lld", ticketID] params:[NSMutableDictionary dictionaryWithObject:[NSNumber numberWithLongLong:ticketID] forKey:@"ticketID"] delegate:self];
//                }
//            }
//        }
//        else
//        {
//            CHJAlertView* waittingAlertView = [[CHJAlertView alloc] init];
//            [waittingAlertView show];
//            [waittingAlertView setTitle:[xml getStringForKey:@"error" defaultValue:[FilePack getStringByID:STR_ERROR_OCCUR]] max_width:160 animated:YES];
//            [waittingAlertView closeInTime:2];
//            [waittingAlertView release];
//            waittingAlertView = nil;
//        }
//    }
//}
//
//- (void)requestFailed:(ASIHTTPRequest *)request
//{
//    if (request.userInfo == nil)
//    {
//        CHJAlertView* waittingAlertView = [[CHJAlertView alloc] init];
//        [waittingAlertView show];
//        [waittingAlertView setTitle:[FilePack getStringByID:STR_ERROR_OCCUR] max_width:160 animated:YES];
//        [waittingAlertView closeInTime:2];
//        [waittingAlertView release];
//        waittingAlertView = nil;
//    }
//}

- (void)refresh
{
    for (ASIHTTPRequest* request in get_logo_requests)
    {
        [[AppDelegate sharedInstance] cancelRequest:request];
    }
    for (ASIHTTPRequest* request in get_batchWeight_requests)
    {
        [[AppDelegate sharedInstance] cancelRequest:request];
    }
    for (ASIHTTPRequest* request in get_plat_addr_requests)
    {
        [[AppDelegate sharedInstance] cancelRequest:request];
    }
    for (ASIHTTPRequest* request in get_emails_requests)
    {
        [[AppDelegate sharedInstance] cancelRequest:request];
    }
    [get_logo_requests removeAllObjects];
    [get_batchWeight_requests removeAllObjects];
    [get_plat_addr_requests removeAllObjects];
    [get_emails_requests removeAllObjects];
    [[AppDelegate sharedInstance] cancelRequest:refresh_request];
    
    [bwc cancel]; [bwc release]; bwc = nil;
    [bwd release]; bwd = nil;
    bwid = 0;
    
    
    NSMutableDictionary* param = [NSMutableDictionary dictionary];
    [param setValue:@"refresh" forKey:@"type"];
    refresh_request = [[AppDelegate sharedInstance] sendHttpRequest:[NSString stringWithFormat:@"getTicketList?Truckid=%@", [AppDelegate sharedInstance].truckID] params:param delegate:self];

//    [normalQuene cancelAllOperations];
//    
//    NSArray* servers = [LoginServer sharedInstance].data;
//    
//    for (NSDictionary* dic in servers)
//    {
//        NSString* host = [dic valueForKey:@"url"];
//        NSString* api = [NSString stringWithFormat:@"%@/getTicketList", host];
//        NSURL *url = [NSURL URLWithString:api];
//        ASIHTTPRequest* request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
//        [normalQuene addOperation:request];
//    }
}

- (void)saveCustomAndNotifyChange
{
    [[NSNotificationCenter defaultCenter] postNotificationName:DataSourceChangeNotification object:self];
}


-(void)requestFinished:(NSMutableDictionary*)params data:(XMLObject*)xml succ:(BOOL)succ
{
    if ([[params valueForKey:@"type"] isEqual:@"refresh"])
    {
        refresh_request = nil;
        XMLObject* tickets = [xml getXMLObjectForKey:@"Tickets"];
        NSMutableArray* ser = [NSMutableArray array];
        for (int i = 0; i < [tickets getCount]; i++)
        {
            XMLObject* obj = [tickets getXMLObjectForIndex:i];
            NSMutableDictionary* sub = [obj convert2Dictionary];
            [sub setValue:[[obj getXMLObjectForKey:@"Products"] convert2Array] forKey:@"Products"];
            [ser addObject:sub];
        }
        
        for (NSMutableDictionary* dic in ser)
        {
            NSString* date = [dic valueForKey:@"Date"];
            NSDateFormatter *inputFormat = [[NSDateFormatter alloc] init];
            [inputFormat setAMSymbol:@"AM"];
            [inputFormat setPMSymbol:@"PM"];
            [inputFormat setDateFormat:@"M/d/yyyy hh:mm:ss a"]; //"8/14/2012 12:00:00 AM"
            
            NSDate *theDate  = [inputFormat dateFromString:date];
            
            long long ctime = [theDate timeIntervalSince1970];
            [dic setValue:[NSNumber numberWithLongLong:ctime] forKey:@"ctime"];
        }

        if ([xml getIntForKey:@"ret" defaultValue:0] == 1)
        {
            NSMutableArray* changed = [NSMutableArray array];
            for (int i = 0; i < [ser count]; i++)
            {
                NSDictionary* dic = [ser objectAtIndex:i];
                long long tickedIDi = [[dic valueForKey:@"TicketID"] longLongValue];
                BOOL find = NO;
                for (int j = 0; j < [data count]; j++)
                {
                    NSDictionary* jd = [data objectAtIndex:j];
                    long long tickedIDj = [[jd valueForKey:@"TicketID"] longLongValue];
                    
                    if (tickedIDi == tickedIDj)
                    {
                        [data replaceObjectAtIndex:j withObject:dic];
                        find = YES;
                        break;
                    }
                }
                if (find == NO)
                {
                    [data addObject:dic];
                }
                [changed addObject:[NSNumber numberWithLongLong:tickedIDi]];
                
                if ([memoryObject valueForKey:[NSString stringWithFormat:@"%lld", tickedIDi]] == nil)
                {
                    [memoryObject setValue:[NSMutableDictionary dictionary] forKey:[NSString stringWithFormat:@"%lld", tickedIDi]];
                }
            }
            
            for (NSDictionary* dic in data)
            {
                long long ticketID = [[dic valueForKey:@"TicketID"] longLongValue];
                NSMutableDictionary* c = [memoryObject objectForKey:[NSString stringWithFormat:@"%lld", ticketID]];
                NSString* logo = [c valueForKey:LOGO_URL];
                if (logo == nil)
                {
                    ASIHTTPRequest* get_logo_request = [[AppDelegate sharedInstance] sendHttpRequest:[NSString stringWithFormat:@"getLogo?&ticketID=%lld", ticketID] params:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithLongLong:ticketID], @"ticketID", @"getLogo", @"type", nil] delegate:self];
                    [get_logo_requests addObject:get_logo_request];
                }
                
                NSString* plat_addr = [c valueForKey:PLANT_ADDR];
                if (plat_addr == nil)
                {
                    ASIHTTPRequest* get_plat_addr_request = [[AppDelegate sharedInstance] sendHttpRequest:[NSString stringWithFormat:@"getPlantAdd?&ticketID=%lld", ticketID] params:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithLongLong:ticketID], @"ticketID", @"getPlatAdd", @"type", nil] delegate:self];
                    [get_batchWeight_requests addObject:get_plat_addr_request];
                }
                
                NSMutableArray* emails = [c valueForKey:EMAILS];
                if (emails == nil)
                {
                    ASIHTTPRequest* get_email = [[AppDelegate sharedInstance] sendHttpRequest:[NSString stringWithFormat:@"getEmails?&ticketID=%lld", ticketID] params:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithLongLong:ticketID], @"ticketID", @"getEmails", @"type", nil] delegate:self];
                    [get_emails_requests addObject:get_email];
                }
                
                
                NSString* address1 = [dic valueForKey:@"DeliveryAddress1"];
                NSString* address2 = [dic valueForKey:@"DeliveryAddress2"];
                //NSString* post = [dic valueForKey:@"post"];
                
                if (address1 == nil)address1 = @"";
                if (address2 == nil)address2 = @"";
                //if (post == nil)post = @"";
                
                NSString* address = [NSString stringWithFormat:@"%@ %@", address1, address2];
                [dic setValue:address forKey:@"chj_address"];
                [[GoogleMapDownloader sharedInstance] downloadMap:address];
            }
            
            
            
            
            
            
            
            BOOL haveBatchWeight = NO;
            for (NSDictionary* dic in data)
            {
                if ([[dic valueForKey:DONT_NEED_BW] intValue] == 0)
                {
                    long long ticketID = [[dic valueForKey:@"TicketID"] longLongValue];
                    NSMutableDictionary* c = [memoryObject objectForKey:[NSString stringWithFormat:@"%lld", ticketID]];
                    if ([c objectForKey:BATCH_WEIGHT])
                    {
                        haveBatchWeight = YES;
                        break;
                    }
                }
            }
            if (haveBatchWeight == NO)
            {
                for (NSDictionary* dic in data)
                {
                    if ([[dic valueForKey:DONT_NEED_BW] intValue] == 0)
                    {
                        long long ticketID = [[dic valueForKey:@"TicketID"] longLongValue];
                        ASIHTTPRequest* batch_weight_request = [[AppDelegate sharedInstance] sendHttpRequest:[NSString stringWithFormat:@"getBatchWeight?&ticketID=%lld", ticketID] params:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithLongLong:ticketID] ,@"ticketID", @"BATCH_WEIGHT", @"type", nil] delegate:self];
                        [get_batchWeight_requests addObject:batch_weight_request];
                    }
                }
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:DataSourceChangeNotification object:self userInfo:[NSDictionary dictionaryWithObject:changed forKey:@"changed"]];
        }
        else
        {
            CHJAlertView* waittingAlertView = [[CHJAlertView alloc] init];
            [waittingAlertView show];
            [waittingAlertView setTitle:[xml getStringForKey:@"error" defaultValue:[FilePack getStringByID:STR_ERROR_OCCUR]] max_width:160 animated:YES];
            [waittingAlertView closeInTime:2];
            [waittingAlertView release];
            waittingAlertView = nil;
        }
    }
    else if ([[params valueForKey:@"type"] isEqual:@"getLogo"])
    {
        long long ticketID = [[params valueForKey:@"ticketID"] longLongValue];
        if ([xml getIntForKey:@"ret" defaultValue:0] == 1)
        {
            NSMutableDictionary* c = [memoryObject objectForKey:[NSString stringWithFormat:@"%lld", ticketID]];
            if (c == nil)
            {
                c = [NSMutableDictionary dictionary];
                [memoryObject setObject:c forKey:[NSString stringWithFormat:@"%lld", ticketID]];
            }
            NSString* logo = [xml getStringForKey:@"logo" defaultValue:nil];
            if (logo)
            {
                [c setValue:logo forKey:LOGO_URL];
                [[NSNotificationCenter defaultCenter] postNotificationName:DataSourceChangeNotification object:self userInfo:[NSDictionary dictionaryWithObject:[NSArray arrayWithObject:[NSNumber numberWithLongLong:ticketID]] forKey:@"changed"]];
                [[ImageDownloader sharedInstance] downloadImage:logo];
            }
        }
    }
    else if ([[params valueForKey:@"type"] isEqual:@"getPlatAdd"])
    {
        long long ticketID = [[params valueForKey:@"ticketID"] longLongValue];
        if ([xml getIntForKey:@"ret" defaultValue:0] == 1)
        {
            NSMutableDictionary* c = [memoryObject objectForKey:[NSString stringWithFormat:@"%lld", ticketID]];
            if (c == nil)
            {
                c = [NSMutableDictionary dictionary];
                [memoryObject setObject:c forKey:[NSString stringWithFormat:@"%lld", ticketID]];
            }
            NSString* plat_addr = [xml getStringForKey:@"Address" defaultValue:nil];
            if (plat_addr)
            {
                [c setValue:plat_addr forKey:PLANT_ADDR];
                [[NSNotificationCenter defaultCenter] postNotificationName:DataSourceChangeNotification object:self userInfo:[NSDictionary dictionaryWithObject:[NSArray arrayWithObject:[NSNumber numberWithLongLong:ticketID]] forKey:@"changed"]];
            }
        }
    }
    else if ([[params valueForKey:@"type"] isEqual:@"getEmails"])
    {
        long long ticketID = [[params valueForKey:@"ticketID"] longLongValue];
        if ([xml getIntForKey:@"ret" defaultValue:0] == 1)
        {
            NSMutableDictionary* c = [memoryObject objectForKey:[NSString stringWithFormat:@"%lld", ticketID]];
            if (c == nil)
            {
                c = [NSMutableDictionary dictionary];
                [memoryObject setObject:c forKey:[NSString stringWithFormat:@"%lld", ticketID]];
            }
            NSArray* emails = [[xml getXMLObjectForKey:@"emails"] convert2Array];
            if (emails)
            {
                [c setValue:emails forKey:EMAILS];
                [[NSNotificationCenter defaultCenter] postNotificationName:DataSourceChangeNotification object:self userInfo:[NSDictionary dictionaryWithObject:[NSArray arrayWithObject:[NSNumber numberWithLongLong:ticketID]] forKey:@"changed"]];
            }
        }
    }
    else if ([[params valueForKey:@"type"] isEqual:@"BATCH_WEIGHT"])
    {
        long long ticketID = [[params valueForKey:@"ticketID"] longLongValue];
        if ([xml getIntForKey:@"ret" defaultValue:0] == 1)
        {
            for (ASIHTTPRequest* request in get_batchWeight_requests)
            {
                [[AppDelegate sharedInstance] cancelRequest:request];
            }
            [get_batchWeight_requests removeAllObjects];
            

            {
                NSString* url = [xml getStringForKey:@"Ticket" defaultValue:nil];
                if (url)
                {
                    if ([url isEqualToString:@"Hide"])
                    {
                        NSMutableDictionary* c = [memoryObject objectForKey:[NSString stringWithFormat:@"%lld", ticketID]];
                        [c setValue:@"" forKey:BATCH_WEIGHT];
                        [c setValue:[NSNumber numberWithInt:1] forKey:HIDE_BATCH_WEIGHT];
                        
                        NSMutableArray* changed = [NSMutableArray array];
                        [changed addObject:[NSNumber numberWithLongLong:ticketID]];
                        [[NSNotificationCenter defaultCenter] postNotificationName:DataSourceChangeNotification object:self userInfo:[NSDictionary dictionaryWithObject:changed forKey:@"changed"]];
                        
                        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:[FilePack getStringByID:STR_READY_TO_GO] delegate:nil cancelButtonTitle:[FilePack getStringByID:STR_OK] otherButtonTitles: nil];
                        [alert show];
                        [alert release];
                    }
                    else
                    {
                        NSArray* servers = [LoginServer sharedInstance].data;
                        for (NSDictionary* dic in servers)
                        {
                            NSString* server = [dic valueForKey:@"url"];
                            
                            if ([[dic valueForKey:@"connected"] boolValue])
                            {
                                url = [NSString stringWithFormat:@"%@/%@", server, url];
                                break;
                            }
                        }
                        
                        bwid = ticketID;
                        bwd = [[NSMutableData alloc] initWithCapacity:100];
                        
                        NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
                        [request setURL:[NSURL URLWithString:url]];
                        [request setTimeoutInterval:10];
                        [request setHTTPMethod:@"GET"];
                        
                        bwc = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
                        [bwc scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
                        [bwc start];
                        [request release];

                    }
                }
            }
        }
    }
}


#pragma mark NSURLConnection (delegate)

- (void)connection:(NSURLConnection *)aConnection didReceiveData:(NSData *)_data
{
    if (aConnection == bwc)
    {
        [bwd appendData:_data];
    }
}

- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
{
    if (aConnection == bwc)
    {
        char temp[0]; temp[0] = 0;
        [bwd appendBytes:temp length:1];
        NSString* txt = [NSString stringWithUTF8String:[bwd bytes]];
        [bwd release]; bwd = nil;
        [bwc release]; bwc = nil;
        
        if (txt)
        {
            NSMutableDictionary* c = [memoryObject objectForKey:[NSString stringWithFormat:@"%lld", bwid]];
            [c setValue:txt forKey:BATCH_WEIGHT];
            
            NSMutableArray* changed = [NSMutableArray array];
            [changed addObject:[NSNumber numberWithLongLong:bwid]];
            [[NSNotificationCenter defaultCenter] postNotificationName:DataSourceChangeNotification object:self userInfo:[NSDictionary dictionaryWithObject:changed forKey:@"changed"]];
            
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:[FilePack getStringByID:STR_READY_TO_GO] delegate:nil cancelButtonTitle:[FilePack getStringByID:STR_OK] otherButtonTitles: nil];
            [alert show];
            [alert release];
        }
        
        bwid = 0;
    }
}

- (void)connection:(NSURLConnection *)aConnection didFailWithError:(NSError *)error
{
    [bwd release]; bwd = nil;
    [bwc release]; bwc = nil;
    bwid = 0;
}
@end
